package org.zdc.pivot.wtk;
import nme.text.TextField;
import org.zdc.core.IllegalArgumentException;
import org.zdc.pivot.wtk.listener.ListenerList;
import org.zdc.pivot.wtk.listener.WTKListenerList;
import org.zdc.pivot.wtk.listener.ILabelListener;
import org.zdc.pivot.wtk.listener.ILabelBindingListener;

class Label extends Component
{
	private var text:String;
	private var textfiled:TextField;
	private var textKey:String;
	private var textBindType:BindType;
	private var textBindMapping:ITextBindMapping;
	private var labelListeners:LabelListenerList;
	private var labelBindingListeners:LabelBindingListenerList;
	
	public function new(text:String) 
	{
		super();
		
		textBindType = BindType.BOTH;
		textBindMapping = null;
		labelListeners = new LabelListenerList();
		labelBindingListeners = new LabelBindingListenerList();
		
		this.text = text;
		textfiled = new TextField();
		addChild(textfiled);
		
		installSkin(Label);
	}
	
	public function getText():String {
		return text;
	}
	
	public function getTextWidth():Float {
		return textfiled.textWidth;
	}
	
	public function getTextHeight():Float {
		return textfiled.textHeight;
	}
	
	public function setText(text:String):Void {
		this.text = text;
		textfiled.text = text;
	}
	
	public function getTextKey():String {
		return textKey;
	}
	
	public function setTextKey(textKey:String):Void {
		var previousTextKey:String = this.textKey;

        if (previousTextKey != textKey) {
            this.textKey = textKey;
            labelBindingListeners.textKeyChanged(this, previousTextKey);
        }
	}
	
	public function getTextBindType():BindType {
		return textBindType;
	}
	
	public function setTextBindType(textBindType:BindType):Void {
		if (textBindType == null) {
            throw new IllegalArgumentException();
        }

        var previousTextBindType:BindType = this.textBindType;

        if (previousTextBindType != textBindType) {
            this.textBindType = textBindType;
            labelBindingListeners.textBindTypeChanged(this, previousTextBindType);
        }
	}
	
	public function getTextBindMapping():ITextBindMapping {
		return textBindMapping;
	}
	
	public function setTextBindMapping(textBindMapping:ITextBindMapping):Void {
		var previousTextBindMapping:ITextBindMapping = this.textBindMapping;

        if (previousTextBindMapping != textBindMapping) {
            this.textBindMapping = textBindMapping;
            labelBindingListeners.textBindMappingChanged(this, previousTextBindMapping);
        }
	}
	
	override public function clear():Void {
		if (textKey != null) {
			setText(null);
		}
	}
	
	public function getLabelListeners():ListenerList<ILabelListener> {
		return labelListeners;
	}
	
	public function getLabelBindingListeners():ListenerList<ILabelBindingListener> {
		return labelBindingListeners;
	}
}

class LabelListenerList extends WTKListenerList<ILabelListener>, implements ILabelListener
{
	public function textChanged(label:Label, previousText:String):Void {
		for (l in this) {
			l.textChanged(label, previousText);
		}
	}
}

class LabelBindingListenerList extends WTKListenerList<ILabelBindingListener>, implements ILabelBindingListener
{
	public function textKeyChanged(label:Label, previousTextKey:String):Void {
		for (l in this) {
			l.textKeyChanged(label, previousTextKey);
		}
	}
	
	public function textBindTypeChanged(label:Label, previousTextBindType:BindType):Void {
		for (l in this) {
			l.textBindTypeChanged(label, previousTextBindType);
		}
	}
	
	public function textBindMappingChanged(label:Label, previousTextBindMapping:ITextBindMapping):Void {
		for (l in this) {
			l.textBindMappingChanged(label, previousTextBindMapping);
		}
	}
}